package com.klei.controller;

import com.klei.pojo.UserAdmin;
import com.klei.service.IUserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserAdminController {

    @Autowired
    private IUserAdminService userAdminService;

    @GetMapping("/getUserAdminByUP")
    @ResponseBody
    public boolean getUserAdminByUP(@RequestParam(name = "username") String username,
                                    @RequestParam(name = "password") String password,
                                    HttpServletRequest servletRequest){
        UserAdmin userAdmin = userAdminService.getUserAdminByUP(username, password);

        System.out.println(userAdmin);

        if (userAdmin != null) {
            servletRequest.getSession().setAttribute("userAdmin", userAdmin);
            return true;
        } else {
            servletRequest.getSession().setAttribute("userAdmin", null);
            return false;
        }
    }

    @GetMapping("/getSessionUser")
    @ResponseBody
    public UserAdmin getSessionUser(HttpServletRequest servletRequest){

        return (UserAdmin) servletRequest.getSession().getAttribute("userAdmin");
    }

}
